﻿#include "BoneLord.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    BoneLord::BoneLord(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool BoneLord::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void BoneLord::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool range = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ActionTime = getEnvir()->getTime() + 300;

        if (range)
        {
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->TargetID = getTarget()->ObjectID;
            tempVar->Type = 0;
            Broadcast(tempVar);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step
            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            MonsterObject::Attack();
        }
    }

    void BoneLord::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (Stats[Stat::HP] >= 3)
        {
            unsigned char stage = static_cast<unsigned char>(HP / (Stats[Stat::HP] / 3));

            if (stage < _stage)
            {
                SpawnSlaves();
                _stage = stage;
                return;
            }
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }

    void BoneLord::SpawnSlaves()
    {
        int count = std::min(8, 40 - SlaveList.size());

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->Type = 1;
        Broadcast(tempVar);
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        for (int i = 0; i < count; i++)
        {
            MonsterObject *mob = nullptr;
            switch (getEnvir()->Random->Next(4))
            {
                case 0:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BoneMonster1));
                    break;
                case 1:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BoneMonster2));
                    break;
                case 2:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BoneMonster3));
                    break;
                case 3:
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::BoneMonster4));
                    break;
            }

            if (mob == nullptr)
            {
                continue;
            }

            if (!mob->Spawn(getCurrentMap(), getFront()))
            {
                mob->Spawn(getCurrentMap(), getCurrentLocation());
            }

            mob->setTarget(getTarget());
            mob->ActionTime = getEnvir()->getTime() + 2000;
            SlaveList.push_back(mob);
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }
}
